package com.zhongwang.business.module.baseCostManage.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputImportCostDao;
import com.zhongwang.business.module.baseCostManage.model.InputMachiningModel;
import com.zhongwang.business.module.baseCostManage.model.InputOutsourcingModel;
import com.zhongwang.business.module.baseCostManage.model.VwEquModel;
import com.zhongwang.business.util.ShiroUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  外购件计划数据导入 listener
 * @author kxx
 * @date 2020年10月21日15:47
 */
public class InputOutsourcingListener extends AnalysisEventListener<InputOutsourcingModel> {


    private InputImportCostDao inputImportCostDao;
    private ImportCallBack callBack;
    private List<InputOutsourcingModel> inputOutsourcingModel = new ArrayList<>();
//    private List<VwEquModel> equ;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;
    private InputOutsourcingModel inputOutsourcing;

    public InputOutsourcingListener(InputOutsourcingModel inputOutsourcing, InputImportCostDao inputImportCostDao, ImportCallBack callBack) {
        this.inputImportCostDao = inputImportCostDao;
        this.callBack = callBack;
        this.inputOutsourcing = inputOutsourcing;
    }


    @Override
    public void invoke(InputOutsourcingModel data, AnalysisContext context) {

//        VwEquModel equipment = getName(data.getMacUnitType());
        data.setKind(inputOutsourcing.getInputKind());
        data.setPartName(inputOutsourcing.getInputPartName());
        if (data.getOutMalDesc() == null || data.getOutMalDesc().trim().equals("")) {
            data.setReason(data.getReason() + "物料描述没填");
        }
        if (data.getOutNum() == null) {
            data.setReason(data.getReason() + "数量没填");
        }
        else if (data.getOutNum() <= 0) {
            data.setReason(data.getReason() + "数量必须大于0");
        }
        if (data.getOutUnit() == null || data.getOutUnit().trim().equals("")) {
            data.setReason(data.getReason() + "单位没填");
        }
        if (inputImportCostDao.selectOutsourcingRepetition(data) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }
         /**判断并输出验证结果*/
        if (data.getReason()!= null && data.getReason().length() > 0) {
            inputOutsourcingModel.add(data);
            i++;
            return;
        }
        User user = ShiroUtils.getUser();
        data.setInputUser(user.getName());
        data.setEditDate(new Date());
        data.setFkId(inputOutsourcing.getKbId());

        data.setDesignType(inputOutsourcing.getInputDesignType());
        data.setFkInputId(inputOutsourcing.getInputMacId());

        data.setCostKind(inputOutsourcing.getInputCostKind());
        data.setFkCode(inputOutsourcing.getInputCode());
        data.setIsReceive(0);
        data.setOutDeleteFlag(0);
        data.setOutReportType(1);
        inputImportCostDao.insertOutsourcing(data);
        i++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionList.size() > 0) {
            InputOutsourcingModel model = new InputOutsourcingModel();
            model.setOutMalDesc("第" + exceptionList.toString() + "行数据格式不正确");
            inputOutsourcingModel.add(model);
        }
        if (inputOutsourcingModel.size() > 0) {
            callBack.onWarning(inputOutsourcingModel);
        } else {
            callBack.onSuccess();
        }
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        String err = exception.getMessage();
        exceptionList.add(i);
        i++;
        return;
    }

//    private VwEquModel getName(String equTypes) {
//        for (VwEquModel equipment : equ) {
//            if (equipment.getName() != null && equipment.getName().equals(equTypes)) {
//                return equipment;
//            }
//        }
//        return null;
//    }

}
